#define _CRT_SECURE_NO_WARNINGS 1

//https://leetcode.cn/problems/combination-sum/

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    int _target;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        _target = target;
        dfs(candidates, target, 0);
        return ret;
    }

    void dfs(vector<int>& candidates, int target, int start)
    {
        if (target < 0) return;
        if (target == 0)
        {
            ret.push_back(path);
            return;
        }

        for (int i = start; i < candidates.size(); i++)
        {
            path.push_back(candidates[i]);
            dfs(candidates, target - candidates[i], i);
            path.pop_back();
        }
    }
};